#include "d3d.h"
#include "d3d_util.h"
#include "interface_types.h"
#include "..\engine\log.h"
#include "..\engine\error.h"
#include "..\engine\engine.h"
#include "..\engine\camera.h"
#include "..\input\dinput.h"
#include "..\math\math.h"
#include "..\math\sphere.h"

uint cullcount = 0;
//
//bool CD3D9Device::BoundSphereInFrustum(ViewOrrientation* view, Spheref* pBoundsSphere)
//{
//	Vector3f *pCenter = &pBoundsSphere->m_center;
//	const float radius = pBoundsSphere->m_radius;
//		CEngine::GetInstance()->GetRenderer()->WriteLine(ScreenText::BOLD_FACE,"%i", cullcount);
//
//	if(D3DXVec3Length(&D3DXVECTOR3(view->m_Position.x - pCenter->x,view->m_Position.y - pCenter->y,view->m_Position.z - pCenter->z)) > radius) {
//	
//		D3DXPLANE *pl; 
//		
//		for(uint i = 0; i<6; i++) {
//			
//			pl = &view->m_frustrumPlanes[i]; 
//
//			Planef p(pl->a,pl->b,pl->c,pl->d);
//
//			const Planef::Classification c = Planef::ClassifySphere(&p, pBoundsSphere);
//
//	
//			if(c == Planef::ABOVE){
//				cullcount++;
//				CEngine::GetInstance()->GetRenderer()->DrawSphere(pBoundsSphere->m_center , pBoundsSphere->m_radius);
//				return false;
//			}
//			
//			/*if(0.0f > pl->a * pCenter->x + pl->b * pCenter->y + pl->c * pCenter->z + pl->d + radius)
//			{
//				CEngine::GetInstance()->GetRenderer()->DrawSphere(pBoundsSphere->m_center , pBoundsSphere->m_radius);
//				cullcount ++;
//				return false;
//			}*/
//
//		}
//	}
//	return true; 
//}
//
//bool CD3D9Device::PointsInFrustum(ViewOrrientation* view,Vector3f* p, uint count)
//{
//	D3DXPLANE *pl; 
//	for(int j = 0; j < count; j++)
//	{
//		for(int i = 0;  i < 6 ; i++)
//		{
//			pl = &view->m_frustrumPlanes[i]; 
//			if(0.0f > pl->a * p[j].x + pl->b * p[j].y + pl->c * p[j].z + pl->d)
//				return false; 
//		}
//	}
//	return true; 
//
//}
//
//bool CD3D9Device::PointInFrustum(ViewOrrientation* view,D3DXVECTOR3 &p)
//{
//	D3DXPLANE *pl; 
//	for(int i = 0;  i < 6 ; i++)
//	{
//		pl = &view->m_frustrumPlanes[i]; 
//		if(0.0f > pl->a * p.x + pl->b * p.y + pl->c * p.z + pl->d)
//			return false; 
//	}
//
//	return true; 
//}

void CD3D9Device::UpdateViewFrustum(ViewOrrientation& view)
{	
	D3DXMATRIX frust; 
	D3DXMatrixMultiply(&frust,&view.m_View,&view.m_Projection); 
	Planef *pl; 
	Vector3f *n;

	pl = &view.m_frustrumPlanes[ViewOrrientation::LEFT_PLANE]; 
	n = &pl->normal;
	n->x = frust._14 + frust._11; 
	n->y = frust._24 + frust._21; 
	n->z = frust._34 + frust._31; 
	pl->distance = frust._44 + frust._41;

	pl = &view.m_frustrumPlanes[ViewOrrientation::RIGHT_PLANE]; 
	n = &pl->normal;
	n->x = frust._14 - frust._11; 
	n->y = frust._24 - frust._21; 
	n->z = frust._34 - frust._31; 
	pl->distance = frust._44 - frust._41;

	pl = &view.m_frustrumPlanes[ViewOrrientation::BOTTOM_PLANE]; 
	n = &pl->normal;
	n->x = frust._14 + frust._12; 
	n->y = frust._24 + frust._22; 
	n->z = frust._34 + frust._32; 
	pl->distance = frust._44 + frust._42; 

	pl = &view.m_frustrumPlanes[ViewOrrientation::TOP_PLANE]; 
	n = &pl->normal;
	n->x = frust._14 - frust._12; 
	n->y = frust._24 - frust._22; 
	n->z = frust._34 - frust._32; 
	pl->distance = frust._44 - frust._42;

	pl = &view.m_frustrumPlanes[ViewOrrientation::NEAR_PLANE]; 
	n = &pl->normal;
	n->x = frust._13; 
	n->y = frust._23; 
	n->z = frust._33; 
	pl->distance = frust._43; 

	pl = &view.m_frustrumPlanes[ViewOrrientation::FAR_PLANE]; 
	n = &pl->normal;
	n->x = frust._14 - frust._13; 
	n->y = frust._24 - frust._23; 
	n->z = frust._34 - frust._33; 
	pl->distance = frust._44 - frust._43; 

	for(int i=0;  i < 6; i++) {
		view.m_frustrumPlanes[i].normalize();
	}
}

